package pixelwaks.gui.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import common.i18n.ChangeLanguage;
import common.i18n.IChangeLanguage;
import common.util.FileHandler;

import pixelwaks.ctrl.ControlFactory;
import pixelwaks.ctrl.impl.ModeCtrl;

public class EditSettingsPanel extends JPanel implements IChangeLanguage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String modeMaps;
	private JLabel title = new JLabel();

	private JPanel wRadioPanel = new JPanel();
	private JPanel mChoosePanel = new JPanel();
	private JPanel gCheckPanel = new JPanel();

	private TitledBorder sgsWBorder = BorderFactory.createTitledBorder("");
	private TitledBorder sgsMBorder = BorderFactory.createTitledBorder("");
	private TitledBorder sgsGBorder = BorderFactory.createTitledBorder("");
	private Border sgsSBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);

	private JRadioButton sgsWLow  = new JRadioButton();
	private JRadioButton sgsWMed  = new JRadioButton();
	private JRadioButton sgsWHigh = new JRadioButton();
	private ButtonGroup group = new ButtonGroup();

	private List<String> maps;
	private JComboBox sgsCMap; 

	private String[] weapons;
	private JComboBox sgsFWeapon;
	private JComboBox sgsSWeapon;
	private JComboBox sgsTWeapon;

	private JButton sgsCButton = new JButton();
	private JButton sgsBButton = new JButton();

	private String[] forcedWeapons;
	private List<JComboBox> weaponComboBoxes;

	public EditSettingsPanel(String modeMaps) {
		this.modeMaps = modeMaps;
		ChangeLanguage.addObserver(this);
		initComponents();
		initControls();
		setLanguage(ChangeLanguage.currentLanguage);
	}

	private void initComponents() {


		SpringLayout layout = new SpringLayout();
		setLayout(layout);
		setBorder(sgsSBorder);

		sgsWLow.setBackground(Color.white);
		sgsWMed.setBackground(Color.white);
		sgsWHigh.setBackground(Color.white);
		sgsWMed.setSelected(true);


		group.add(sgsWLow);
		group.add(sgsWMed);
		group.add(sgsWHigh);


		maps = FileHandler.getMapsOfMode(modeMaps);
		ModeCtrl mctrl = (ModeCtrl) ControlFactory.getObserver(ControlFactory.MODE_CTRL);
		weapons = FileHandler.getThisModesWeaponsAsStrings(mctrl.getSelectedMode());
		forcedWeapons = FileHandler.getThisModesForcedWeaponsAsStrings(mctrl.getSelectedMode());

		sgsCMap = new JComboBox(maps.toArray());
		sgsCMap.setSize(new Dimension(100,10));
		sgsFWeapon = new JComboBox(weapons.clone());
		sgsSWeapon = new JComboBox(weapons.clone());
		sgsTWeapon = new JComboBox(weapons.clone());	
		weaponComboBoxes = new ArrayList<JComboBox>();
		weaponComboBoxes.add(sgsFWeapon);
		weaponComboBoxes.add(sgsSWeapon);
		weaponComboBoxes.add(sgsTWeapon);

		wRadioPanel.setBorder(sgsWBorder);
		wRadioPanel.setLayout(new BorderLayout());
		wRadioPanel.setBackground(Color.white);
		wRadioPanel.setPreferredSize(new Dimension(100,100));
		wRadioPanel.add(sgsWLow, BorderLayout.NORTH);
		wRadioPanel.add(sgsWMed, BorderLayout.CENTER);
		wRadioPanel.add(sgsWHigh, BorderLayout.SOUTH);

		gCheckPanel.setBorder(sgsGBorder);
		gCheckPanel.setLayout(new BorderLayout());
		gCheckPanel.setBackground(Color.white);
		gCheckPanel.setPreferredSize(new Dimension(250,100));
		gCheckPanel.add(sgsFWeapon, BorderLayout.NORTH);
		gCheckPanel.add(sgsSWeapon, BorderLayout.CENTER);
		gCheckPanel.add(sgsTWeapon, BorderLayout.SOUTH);

		mChoosePanel.setBorder(sgsMBorder);
		mChoosePanel.setLayout(new BorderLayout());
		mChoosePanel.setBackground(Color.white);
		mChoosePanel.setPreferredSize(new Dimension(280,50));
		mChoosePanel.add(sgsCMap);


		add(title);
		add(wRadioPanel);
		add(gCheckPanel);
		add(mChoosePanel);
		add(sgsCButton);
		add(sgsBButton);

		layout.putConstraint(SpringLayout.WEST, wRadioPanel, 40, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, wRadioPanel, 50, SpringLayout.NORTH, this);

		layout.putConstraint(SpringLayout.WEST, gCheckPanel, 190, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, gCheckPanel, 50, SpringLayout.NORTH, this);

		layout.putConstraint(SpringLayout.WEST, mChoosePanel, 40, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, mChoosePanel, 230, SpringLayout.NORTH, this);

		layout.putConstraint(SpringLayout.WEST, sgsCButton, 350, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, sgsCButton, 222, SpringLayout.NORTH, this);
		
		layout.putConstraint(SpringLayout.WEST, sgsBButton, 350, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, sgsBButton, 252, SpringLayout.NORTH, this);


		setPreferredSize(new Dimension(500,300));
		setVisible(true);
		setBackground(Color.white);
	}

	private void setForcedWeapons() {
		for(int i = 0; i < forcedWeapons.length; i++) {
			weaponComboBoxes.get(i).setSelectedItem(forcedWeapons[i]);
			weaponComboBoxes.get(i).setEnabled(false);
		}
	}
	
	private void initControls() {
		Action a = (Action) ControlFactory.getObserver(ControlFactory.SETTINGS_CTRL);
		sgsWLow.setAction(a);
		sgsWMed.setAction(a);
		sgsWHigh.setAction(a);
		sgsCButton.setAction(a);
		sgsBButton.setAction(a);
		sgsFWeapon.setAction(a);
		sgsSWeapon.setAction(a);
		sgsTWeapon.setAction(a);
		sgsCMap.setAction(a);
		
		setForcedWeapons();
		
		a.putValue("Settings.windLow", sgsWLow);
		a.putValue("Settings.windMed", sgsWMed);
		a.putValue("Settings.windHigh", sgsWHigh);
		a.putValue("Settings.sgsCButton", sgsCButton);
		a.putValue("Settings.sgsBButton", sgsBButton);
		a.putValue("Settings.sgsFWeapon", sgsFWeapon);
		a.putValue("Settings.sgsSWeapon", sgsSWeapon);
		a.putValue("Settings.sgsTWeapon", sgsTWeapon);
		a.putValue("Settings.sgsCMap", sgsCMap);
	}
	@Override
	public void setLanguage(ResourceBundle rb) {
//		ResourceBundle rb = ResourceBundle.getBundle(ChangeLanguage.RESOURCE + "Guitext_" + language, Locale.getDefault(), MyClassLoader.getInstance());
		sgsWLow.setText(rb.getString("sgsWLow"));
		sgsWMed.setText(rb.getString("sgsWMed"));
		sgsWHigh.setText(rb.getString("sgsWHigh"));
		sgsWBorder.setTitle(rb.getString("sgsWBorder"));
		sgsMBorder.setTitle(rb.getString("sgsMBorder"));
		sgsGBorder.setTitle(rb.getString("sgsGBorder"));
		sgsCButton.setText(rb.getString("sgsCButton"));
		sgsBButton.setText(rb.getString("sgsBButton"));
		title.setText(rb.getString("settingsTitle"));
		repaint();
	}

}